
$(function() {
	// _change("input[id='name_s']:visible", "select[id='bank_t']:visible");
	
	$("input[id^='name_s']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'info.php?do=BankCode.ajax_zjm',
				dataType: "json",
				data: {
					bank: $('#bank_s').val(),
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							label: item.name + " [" + item.branch + ", " + item.zjm + "]",
							value: item.branch
						}
					}));
				}
			});
		},
		
		minLength: 1
	});
});

var BankCode = {
	dialog_import: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('info.php?do=BankCode.audit&item=146', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},
		
					"导入": function() {
						if (!confirm("确认是否导入？")) {
							return false;
						}
						
						var form = $("#form_add")[0];
		
						var body = { };					
						var aa = form.way.value;
						
						body.type = form.type.value;
						body.oper = form.oper.value;
						body.data = form.data.value;
						
						if (aa != null && aa == '2') {
							var queryString = $('#form_add').formSerialize(); 
							// alert(queryString);
							
							var s = "info.php?do=BankCode.ajax_upload&" + queryString;
							$('#userfile').uploadifySettings('script', s);
							$('#userfile').uploadifyUpload();
							alert("导入成功！");
							return false;
						}
		
						$(this).dialog("close");
		
						$.post('info.php?do=BankCode.importPost', body, function(data) {
							$(result).html(data);
						});
					}
				});
		
				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});	
	},
		
	dialog_add_: function(url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {	
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_add")[0];
	
				var body = { };
				
				body.type = form.type.value;
				body.name = form.name.value;
				
				body.bank = form.bank.value;
				body.branch = form.branch.value;				
				body.code = form.code.value;
				
				body.content = form.content.value;
				body.status = form.status.value;	
	
				$(this).dialog("close");
	
				$.post('info.php?do=BankCode.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		$.cookie('status_', status);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'info.php?do=BankCode.edit&id=' + id;	
		var c2 = 'info.php?do=BankCode.view&id=' + id;

		var a1 = ['编辑信息', 'url', 'BankCode.dialog_edit_', '600x440'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '600x440'];

		var s = c1 + ", " + c2;

		return popup(e, [a1, a2], s);	
	},
	
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.id = form.id.value;
				
				body.type = form.type.value;
				body.name = form.name.value;
				
				body.bank = form.bank.value;
				body.branch = form.branch.value;				
				body.code = form.code.value;
				
				body.content = form.content.value;
				body.status = form.status.value;

				$(this).dialog("close");
				
				var url = 'info.php?do=BankCode.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	}
};